package com.relytosoft.mizar.media.host.emqx;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.relytosoft.mizar.media.external.api.display.request.CruiseDto;
import com.relytosoft.mizar.media.external.api.display.request.PresetDto;
import com.relytosoft.mizar.media.external.api.display.service.CruisePathService;
import com.relytosoft.mizar.media.external.api.display.service.PresetManagementService;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

import static com.relytosoft.mizar.media.common.constant.MediaMqConstants.DISPLAY_CRUISE_TOPIC;
import static com.relytosoft.mizar.media.common.constant.MediaMqConstants.DISPLAY_PRESET_TOPIC;
import static com.relytosoft.mizar.media.display.services.constants.OperationConstant.*;

/**
 * @author Jierry
 * @version 2022/4/22.
 */
@Component
public class DisplayMqttMessageListener implements com.relytosoft.mizar.media.common.emqx.listener.DisplayMqttMessageListener {

    private static final Logger log = LoggerFactory.getLogger(DisplayMqttMessageListener.class);
    @Resource
    private CruisePathService cruisePathService;

    @Resource
    private PresetManagementService presetManagementService;


    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("线程名称：【" + Thread.currentThread().getName() + "】" + "接收的主题：" + topic);
        try {
            boolean flag = false;
            String mqttMsg = new String(message.getPayload(), StandardCharsets.UTF_8);
            JSONObject msg = JSON.parseObject(mqttMsg);
            String opType = msg.getString("OpType");
            if (StrUtil.equals(topic, DISPLAY_CRUISE_TOPIC)) {
                CruiseDto dto = JSON.parseObject(msg.getString("Data"), CruiseDto.class);
                switch (opType) {
                    case INSERT:
                        flag = cruisePathService.addCruise(dto);
                        break;
                    case UPDATE:
                        flag = cruisePathService.updateCruise(dto);
                        break;
                    case DELETE:
                        flag = cruisePathService.deleteCruise(dto);
                        break;
                    case CONTROL:
                        flag = cruisePathService.controlCruise(dto);
                        break;
                    default:
                }
            } else if (StrUtil.equals(topic, DISPLAY_PRESET_TOPIC)) {
                PresetDto dto = JSON.parseObject(msg.getString("Data"), PresetDto.class);
                switch (opType) {
                    case INSERT:
                        flag = presetManagementService.addPreset(dto);
                        break;
                    case UPDATE:
                        flag = presetManagementService.updatePreset(dto);
                        break;
                    case DELETE:
                        flag = presetManagementService.deletePreset(dto);
                        break;
                    default:
                }
            }
            log.info("线程名称：【" + Thread.currentThread().getName() + "】" + "接收的主题：" + topic + ",接收的操作类型：" + opType + "，执行结果：" + flag);
        } catch (Exception e) {
            log.info("线程名称：【" + Thread.currentThread().getName() + "】" + "接收的主题：" + topic + ",处理发生错误：" + e.getMessage());
        }
    }
}

